@isTest

private class iFulfillAssignWorkflowTest{ 
/** Class Name: iFulfillAssignWorkflowTest
* Author: Accenture
* Date: 14-Sep-2011 
* Requirement/Project Name: iFulfill 
* **/  

        public static QueueSObject q = null;
        public static User u = null;
        static Group g;
        
    static void createQueue(){
         
        Profile p = [select id from profile where name='System Administrator'];                     
        u = new User(alias = 'ipsyas', email='isadmin@accenture.com',emailencodingkey='UTF-8', lastname='TestUser',languagelocalekey='en_US',localesidkey='en_US', profileid = p.Id,timezonesidkey='Asia/Calcutta', username='ifulfilladmin@testorg.com');  
        Insert u;        
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];        
        System.runAs(thisUser) 
        {   
               g = new Group(Type='Queue', Name='TestQueue50');
               insert g;
               GroupMember gm = new GroupMember(GroupId=g.id,UserOrGroupId=UserInfo.getUserId());
               insert gm;
               q = new QueueSObject(SobjectType='Fullfilment_Item__c', QueueId=g.Id);
               insert q;
                
         }  
    }    

  static testMethod void createAssignWorkflowTest()  
  {  
        createQueue();
        Assignment_Group_Name__c AG = new Assignment_Group_Name__c();     
        AG.name = 'AG1';        
        AG.Function__c = 'Advertiser Approval';  
        AG.Type__c = 'Fulfillment';          
        AG.Description__c = 'Description';  
        AG.Round_Robin__c = false; 
        insert AG;
        Assignment_Group_Queues__c AGQ = new Assignment_Group_Queues__c();
        AGQ.Name = 'TestQueue50';
        AGQ.Active__c = 'True';
        AGQ.Assignment_Group_Name__c = AG.id;
        AGQ.Valid_Queue__c = true;
        insert AGQ;
      Workflow_definition__c wd = new Workflow_definition__c(Status__c='Active',Version__c=1.0,Workflow_Name__c='TestWF',Workflow_Description__c='TestDescription');
      insert wd;  
      
      Stage_Definition__c stD = new Stage_Definition__c(name = 'STA',Stage_Description__c = 'Desc',Sequence__c = 83,Status__c = 'Active');     
      insert stD;

      Step_definition__c SD = new Step_definition__c(name = 'Test Step',Stage__c = stD.id,Step_Sequence__c = 55,Target_Days__c = 4,status__c='Active');
      insert SD;
       
      Workflow_Stage__c WS = new Workflow_Stage__c(Name='TestRest',Stage__c=stD.id,Sequence__c=1,Workflow_Code__c=wd.id); 
      insert WS;
    
            
      Wokflow_Steps__c WT= new Wokflow_Steps__c(Step__c=SD.id,Wokflow_Stage_Name__c=WS.id,Sequence__c=1,Target_Days__c=2, Default_Group__c = AG.id);
      insert WT;      
             
      Fulfillment_Product_Group__c FP= new Fulfillment_Product_Group__c(Product_Group_Description__c='PHONE',Product_Group_Code__c='PHONE');
      insert FP;
      
      List<Fulfillment_Products__c> fulfillmentProductList = new List<Fulfillment_Products__c>(); 
      List<Workflow_Fulfillment_Group__c>  workFulfillmentList = new List<Workflow_Fulfillment_Group__c>();  
      Case testIncidentCase = new Case(Description='IOG Incident Case ',Status='Open',Origin='CRF');
      insert testIncidentCase; 
                    
      Fulfillment_Products__c FPD= new Fulfillment_Products__c();
      FPD.Product_Family__c='PHONE';
      FPD.Product_Descritpion__c='PHONE';
      FPD.Package__c=true;
      FPD.Product_Descritpion__c='Product';
      FPD.Feature_Description__c='Test';
      FPD.Fulfillment_Product_Group__c=FP.ID;  
      FPD.Feature__c='F123';
      FPD.Product_UDAC_Code__c='U1234';
      FPD.Status__c='Active';      
      fulfillmentProductList.add(FPD);


       Fulfillment_Products__c FPD1= new Fulfillment_Products__c();
      FPD1.Product_Family__c='PHONE';
      FPD1.Product_Descritpion__c='PHONE';
      FPD1.Package__c=true;
      FPD1.Product_Descritpion__c='Product';
      FPD1.Feature_Description__c='Test';
      FPD1.Fulfillment_Product_Group__c=FP.ID;  
      FPD1.Feature__c='F1234';
      FPD1.Product_UDAC_Code__c='U12345';
      FPD1.Status__c='Active';
      fulfillmentProductList.add(FPD1);

      insert fulfillmentProductList;



      Workflow_Fulfillment_Group__c WF= new Workflow_Fulfillment_Group__c(Workflow__c=wd.id,Product_Group__c=FP.id,Status__c='Active',Order_Types__c='CAN',Advertiser_Region__c='Region3');
      workFulfillmentList.add(WF); 
      
      Workflow_Fulfillment_Group__c WFG= new Workflow_Fulfillment_Group__c(Workflow__c=wd.id,Product_Group__c=FP.id,Status__c='Active',Order_Types__c='CAN1',Advertiser_Region__c='Region4');
      workFulfillmentList.add(WFG); 

      Workflow_Fulfillment_Group__c WFG1= new Workflow_Fulfillment_Group__c(Workflow__c=wd.id,Product_Group__c=FP.id,Status__c='Active',Order_Types__c='Change Request',Advertiser_Region__c='Region5');
      workFulfillmentList.add(WFG1); 

        insert workFulfillmentList;                  

      Fullfilment_Item__c FI= new Fullfilment_Item__c(UDAC__c='U1234',Transaction_Type__c='CAN',Region__c='Region3',Feature_Code__c='F123');      
      FI.Fulfillment_Product_Group__c=FPD.Fulfillment_Product_Group__c; 
      FI.Feature__c= FPD.Feature_Description__c; 
      FI.Package_Multi_feature_Product__c= FPD.Package__c; 
      FI.UDAC_Description__c= FPD.Product_Descritpion__c; 
      FI.Product_Family__c= FPD.Product_Family__c; 
      FI.Workflow_Code__c =WF.Workflow__c; 
      FI.Workflow_Version__c =WF.Workflow__r.Version__c; 
      FI.Workflow_Start_Date__c= System.now(); 
      FI.Status__c = 'OPEN'; 
      FI.Current_Stage__c =WS.ID; 
      FI.Current_Stage_Start_Date__c=System.now(); 
      FI.Current_Step__c =WT.ID; 
      FI.Current_Step_Start_Date__c =System.now(); 
            
      
      Fullfilment_Item__c fIem= new Fullfilment_Item__c(UDAC__c='U12',Transaction_Type__c='CAN',Region__c='Region5',Feature_Code__c='F123');  
       

      Fullfilment_Item__c FI1= new Fullfilment_Item__c(UDAC__c='U12345',Transaction_Type__c='Change Request',Related_Case_Id__c=testIncidentCase.id,Region__c='Region5',Feature_Code__c='F1234');  
           
      List<Fullfilment_Item__c> FIList = new List<Fullfilment_Item__c>();
      FIList.add(FI);
      FIList.add(fIem);
      FIList.add(FI1);
    
     insert FIList;
       iFulfillAssignWorkflow.AssignWorkflow(FIList,true);
      iFulfillAssignWorkflow.createOpenStepHistoryForChangeRequest(FIList); 

      }
       static testMethod void wfAssinment(){          
           
           PageReference pageRef = Page.WFAssignmentPage;
           Test.setCurrentPage(pageRef);
           ApexPages.currentPage().getParameters().put('retUrl', 'apex/testclass');
           ApexPages.currentPage().getHeaders().put('Host', 'https://cs3.salesforce.com');

           List<Fullfilment_Item__c> FIList= new List<Fullfilment_Item__c>(); 
           Fullfilment_Item__c FI= new Fullfilment_Item__c(UDAC__c='U1234',Status__c='Unassigned',Transaction_Type__c='CAN',Region__c='Region3',Feature_Code__c='F123');      
           insert FI;
           FIList.add(FI);
           Fullfilment_Item__c fIem= new Fullfilment_Item__c(UDAC__c='U12',Status__c='Unassigned',Transaction_Type__c='CAN',Region__c='Region5',Feature_Code__c='F123');  
           insert fIem;
           FIList.add(fIem); 
           
           System.debug('FIList'+FIList);                                
           ApexPages.StandardSetController  ctrl = new ApexPages.StandardSetController(FIList);
           System.debug('ctrl '+ctrl);
           ctrl.setSelected(FIList); 
           
           WFAssignment controller = new WFAssignment(ctrl);  
           controller.assignWF();            
                       
      }  
       static testMethod void fulfillClaim(){     
           PageReference pageRef = Page.IFulfillClaimPage;
           Test.setCurrentPage(pageRef);
           ApexPages.currentPage().getParameters().put('retUrl', 'apex/testclass');
           ApexPages.currentPage().getHeaders().put('Host', 'https://cs3.salesforce.com');
           createQueue();
           List<Fullfilment_Item__c> FIList= new List<Fullfilment_Item__c>(); 
           Fullfilment_Item__c FI= new Fullfilment_Item__c(UDAC__c='U1234',Feature_Code__c='F123',transaction_type__c='Cancellation');      
           insert FI;
           Fullfilment_Item__c fIem= new Fullfilment_Item__c(UDAC__c='U12',Transaction_Type__c='CAN',Region__c='Region5',Feature_Code__c='F123');  
           insert fIem;
                     
           FI.Ownerid=g.id;
           update FI;
           System.debug('User Id:'+u.id);
           fIem.Ownerid=u.id;
           update fIem;
           
           FIList.add(FI);

           FIList.add(fIem);
                                
           ApexPages.StandardSetController  ctrl = new ApexPages.StandardSetController(FIList);
           ctrl.setSelected(FIList); 
           IFulfillClaim controller = new IFulfillClaim (ctrl);  
           controller.assignFI(); 
      }    
 static testMethod void ifulfillRestrictDeleteWDefinition(){
     List<Workflow_Definition__c> WDList= new List<Workflow_Definition__c>(); 
     Workflow_definition__c wd = new Workflow_definition__c(Status__c='Inactive',Version__c=1.0,Workflow_Name__c='TestWF',Workflow_Description__c='TestDescription');      
     insert wd; 
     try{
     delete wd; 
     }
     catch (Exception e)
     {}
  }


static testMethod void ifulfillRestrictDeleteWFunction(){
  List<Workflow_Stage__c> WFList= new List<Workflow_Stage__c>(); 
  Workflow_definition__c wd = new Workflow_definition__c(Status__c='Active',Version__c=1.0,Workflow_Name__c='TestWF',Workflow_Description__c='TestDescription');
  insert wd;  
  Stage_Definition__c stD = new Stage_Definition__c(name = 'STA',Stage_Description__c = 'Desc',Sequence__c = 83,Status__c = 'Active');     
  insert stD;
  Workflow_Stage__c WS = new Workflow_Stage__c(Name='TestRest',Stage__c=stD.id,Sequence__c=1,Workflow_Code__c=wd.id); 
  insert WS;
     try{
     delete WS; 
     }
     catch (Exception e)
     {}
}


static testMethod void ifulfillRestrictDeleteWTask(){
  List<Wokflow_Steps__c> WFList= new List<Wokflow_Steps__c>(); 

  Workflow_definition__c wd = new Workflow_definition__c(Status__c='Active',Version__c=1.0,Workflow_Name__c='TestWF',Workflow_Description__c='TestDescription');
  insert wd;           
  Stage_Definition__c stD = new Stage_Definition__c(name = 'STA',Stage_Description__c = 'Desc',Sequence__c = 83,Status__c = 'Active');     
  insert stD;
  Step_definition__c SD = new Step_definition__c(name = 'Test Step',Stage__c = stD.id,Step_Sequence__c = 55,Target_Days__c = 4,status__c='Active');
  insert SD;
  Workflow_Stage__c WS = new Workflow_Stage__c(Name='TestRest',Stage__c=stD.id,Sequence__c=1,Workflow_Code__c=wd.id); 
  insert WS;
  Wokflow_Steps__c WT= new Wokflow_Steps__c(Step__c=SD.id,Wokflow_Stage_Name__c=WS.id,Sequence__c=1,Target_Days__c=2);
  insert WT; 
 try{
     delete WT; 
     }
     catch (Exception e)
     {}
}



static testMethod void ifulfillRestrictDeleteWActivities(){
  List<Workflow_definition__c> WFList= new List<Workflow_definition__c>(); 

  Workflow_definition__c wd = new Workflow_definition__c(Status__c='Active',Version__c=1.0,Workflow_Name__c='TestWF',Workflow_Description__c='TestDescription');
  insert wd;           
  Stage_Definition__c stD = new Stage_Definition__c(name = 'STA',Stage_Description__c = 'Desc',Sequence__c = 83,Status__c = 'Active');     
  insert stD;
  Step_definition__c SD = new Step_definition__c(name = 'Test Step',Stage__c = stD.id,Step_Sequence__c = 55,Target_Days__c = 4,status__c='Active');
  insert SD;
  Activities_Definition__c AD = new Activities_Definition__c(Step__c =SD.id ,Status__c ='Active');
  insert AD;
  Workflow_Stage__c WS = new Workflow_Stage__c(Name='TestRest',Stage__c=stD.id,Sequence__c=1,Workflow_Code__c=wd.id); 
  insert WS;
  Wokflow_Steps__c WT= new Wokflow_Steps__c(Step__c=SD.id,Wokflow_Stage_Name__c=WS.id,Sequence__c=1,Target_Days__c=2);
  insert WT; 
  Workflow_Activities__c WA =new Workflow_Activities__c(Activity__c = AD.id ,status__c='Active');
  insert WA;
  
    WFList =  [select status__c from Workflow_definition__c where id = :wd.id ];
  
    try{
         WFList.get(0).status__c = 'Inactive';
         update WFList.get(0);
         WA.status__c = 'Inactive';
         update WA; 
         delete WA; 
    }
     catch (Exception e)
     {}
 
}


}